package com.ioog.domain.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.ioog.domain.dao.LocationDAO;
import com.ioog.domain.model.Location;

@Repository
public class LocationDAOHibernate implements LocationDAO {

	private SessionFactory sessionFactory;
	
	public LocationDAOHibernate() {}
	 	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void delete(Long id) {
		Location loc = findById(id);
		sessionFactory.getCurrentSession().delete(loc);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Location> findAll() {
		
		return sessionFactory.getCurrentSession()
				.createQuery("from Location")
				.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Location> findByExample(Location exampleInstance, String... excludeProperty) {
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(Location.class);
		Example example = Example.create(exampleInstance);
		for(String exclude : excludeProperty){
			example.excludeProperty(exclude);
		}
		crit.add(example);
		return crit.list();
	}

	@Override
	public Location findById(Long id) {
		
		return (Location) sessionFactory.getCurrentSession().load(Location.class, id);
	}

	@Override
	public void store(Location location) {
		sessionFactory.getCurrentSession().saveOrUpdate(location);

	}

}
